﻿// Module:             Games Programming
// Assignment1:        Platformer Game
// Add game feature:   Enemy that moves up-down
// Student Number:     S10095741D
// Student Name:       Ng Cheong Sian

//////////////////////////////////////////////////////////////////////////


using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Game.EnemyScripts
{
    class UpDownEnemy : IEnemy
    {

        private readonly Texture2D _texture;
        private Vector2 _position;
        private readonly float _minY;
        private readonly float _maxY;
        private readonly float _offset;
        private readonly float _speed;
        private bool _activated = true;

        public UpDownEnemy(Vector2 position, float offset, float speed, Texture2D tex)
        {
            _offset = offset;
            _texture = tex;
            _position = position;
            _speed = speed;
            _minY = _position.Y - offset / 2 - _texture.Width;
            _maxY = _position.Y + offset / 2;
        }

        public void Update(GameTime gameTime)
        {

            if(_activated)
            {
                if(_position.Y > _maxY)
                {
                    _activated = false;
                }
                else
                {
                    _position.Y += _speed;
                }
            }
            else
            {
                if (_position.Y < _minY)
                {
                    _activated = true;
                }
                else
                {
                    _position.Y -= _speed;
                }
            }
        }

        public Rectangle GetBoundingRect()
        {
            var rect = new Rectangle((int) _position.X, (int) _position.Y, _texture.Width, _texture.Height);
            return rect;
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, _position, Color.White);
        }
    }
}
